package com.pw.study.mysql;

import com.google.gson.Gson;
import com.pw.study.common.constants.TopicConstant;
import com.pw.study.handler.KafkaHandler;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

import static jdk.nashorn.internal.objects.NativeArray.forEach;
import static jdk.nashorn.internal.objects.NativeArray.map;

public class UserInfo {
        public static final String URL = "jdbc:mysql://hadoop112:3306/0924?useSSL=false";
        public static final String USER = "root";
        public static final String PASSWORD = "123456";

        public static void main(String[] args) throws Exception {
            //1.加载驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 获得数据库连接
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            //3.操作数据库，实现增删改查
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from user_info");
            //如果有数据，rs.next()返回true
            ResultSetMetaData metaData = rs.getMetaData();
            System.out.println(metaData.getColumnCount());
            System.out.println(metaData.getColumnName(1));

            Gson gson = new Gson();

            System.out.println("查询结果为：");
            ArrayList list = new ArrayList();
            while (rs.next()) {
                HashMap<String, String> user = new HashMap<>();
                for (int i = 1; i < metaData.getColumnCount(); i++) {
                    String name = metaData.getColumnName(i);
                    String vv = rs.getString(name);
                    user.put(name, vv);
                }
                String s = gson.toJson(new HashMap<>(user));
                KafkaHandler.sendDataToKafka(TopicConstant.GMALL_USER_INFO,s);
                user.clear();
                System.out.println(s);
            }
Thread.sleep(4000);
conn.close();


    }
}
